// pages/star/starIndex/starIndex.js
const common = require('../../../utils/util.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        starList: [],
        page: 0,
        limit: 10,
        loadingStatus: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let that = this;
        that.getStarList();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 1000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        let that = this;
        let page = that.data.page;
        let loadingStatus = that.data.loadingStatus;
        if (loadingStatus) {
            page++;
            that.setData({
                page: page
            })
            that.getStarList();
        }
    },
    getStarList: function() {
        let that = this;
        let starList = that.data.starList;
        let loadingStatus = that.data.loadingStatus; // 判断是否执行上拉加载
        let limit = that.data.limit;
        let data = {
            activity: 785,
            type: 4,
            limit: limit,
            page: that.data.page
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Fodder/fodder_list', data).then(function(res) {
            wx.hideLoading()
            if (res.code == 204) {
                if (res.error === "暂无数据") {
                    that.setData({
                        loadingStatus: false
                    })
                }
                return false
            } else {
                if (res.length < limit) { // 上拉判断
                    loadingStatus = false;
                }
                starList = starList.concat(res);
                that.setData({
                    starList: starList,
                    loadingStatus: loadingStatus
                })
                return false
            }

        }).catch(function(e) {})
    },

    goStarDetail: function(e) {
        wx.navigateTo({
            url: '/pages/star/starDetail/starDetail?fodder_id=' + e.currentTarget.dataset.id
        })
    }
})